package com.ebook.server.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ebook.server.service.BookService;
import com.ebook.server.service.BookmarkService;
import com.ebook.server.service.NoteService;
import com.ebook.server.service.UserService;
import com.ebook.util.UpAndDown;

@Controller
@RequestMapping(value = "/book")
public class BookController {

	public static final String UPLOAD_TEMP_PATH = "./";
	@Resource
	private BookService bookService;

	@Resource
	private NoteService noteService;

	@Resource
	private UserService userService;

	@Resource
	private BookmarkService bookmarkService;
	

	/**
	 * TODO 测试本地断点续传下载的代码  --待删除
	*/
//	@RequestMapping(value = "/download")
//	public ModelAndView download_new(HttpServletRequest request, HttpServletResponse response){
//		ModelAndView modelView = new ModelAndView();
//		ModelMap modelMap = new ModelMap();
//		String filePath = "C:\\opt\\ebook\\data\\note\\1\\aa.zip";
//		UpAndDown.continueToDownload(filePath, request, response);
//		return modelView;
//	}
	
	
	@RequestMapping(value = "/getNewBooks")
	public ModelAndView getNewBooks(@RequestParam("uid") int uid, @RequestParam("bookidsJson") String bookidsJson, HttpServletRequest request) {
		return bookService.getNewBooks(uid, bookidsJson, request);

	}


	/**
	 * 获取一本图书详细信息
	 * @param uid
	 * @param bookid
	 * @param request
	 * @return 
	*/
	@RequestMapping(value = "/detail")
	public ModelAndView getDetailInfo(@RequestParam("uid") int uid,@RequestParam("bookid") int bookid, HttpServletRequest request) {		
		return bookService.getBookDetail(uid, bookid, request);
	}
	
	 
	/**
	 * 下载一本图书。要求客户端已经登录，并提交uid，bookid. 格式 /download?uid=xx&bookid=xx
	 * TODO: 用json格式传输电子书文件
	 * @param model
	 * @param request
	 * @return
	 */
//	 @RequestMapping(value = "/download", method = RequestMethod.POST)
//	@RequestMapping(value = "/download_json")
//	public ModelAndView downloadJson(@RequestParam("uid") int uid,@RequestParam("bookid") int bookid, HttpServletRequest request) {
//		ModelAndView modelView = new ModelAndView();
//		Map<String, Object> modelMap = new HashMap<String, Object>();
//		String status = Status.SUCCESS;
//		String bookFileJson = null;
//		User user = (User) request.getSession().getAttribute("loginuser");
//				if (user == null || uid != user.getId()) {
//					return userService.notLoginView(modelView, modelMap);
//				}
//				if (uid == user.getId()) {
//			bookFileJson = bookService.getBookFile_old(bookid);
//			if (bookFileJson == null) {
//				status = Status.REJECTED + ":" + ErrorCode.BOOK_NOT_EXIST;
//			}
//		}
//		modelMap.put("status", status);
//		modelMap.put("bookFile", bookFileJson);
//		modelView.addAllObjects(modelMap);
//		return modelView;
//	}

	/**
	 * TODO   --待删除
	*/
//	@RequestMapping(value = "/download")
//	public ModelAndView download(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid, Model model,
//			HttpServletRequest request, HttpServletResponse response) {
//		ModelAndView modelView = new ModelAndView();
//		Map<String, Object> modelMap = new HashMap<String, Object>();
//		String status = Status.SUCCESS;
//		User user = (User) request.getSession().getAttribute("loginuser");
//				if (user == null || uid != user.getId()) {
//					return userService.notLoginView(modelView, modelMap);
//				}
//				if (uid == user.getId()) {
//			if (!UpAndDown.downloadZip(bookService.getBookFile(uid,bookid), request, response)) {
//				status = Status.REJECTED + ":" + ErrorCode.BOOK_DOWN_FAIL;
//			} else {
//				modelMap.put("bookid", bookid);
//			}
//		}
//		modelMap.put("status", status);
//		modelView.addAllObjects(modelMap);
//		return modelView;
//	}

	/**
	 * 支持断点续传下载用户图书的功能
	 * @param uid      用户id
	 * @param bookid   图书id
	 * @param request
	 * @param response
	 * @return 成功失败信息
	*/
	@RequestMapping(value = "/downloadEbook")
	public ModelAndView downloadEbook(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			HttpServletRequest request, HttpServletResponse response) {
		return bookService.downloadEbook(uid, bookid, request, response);
	}

	/**
	 * 笔记上传功能
	 * @param noteFile  zip文件包
	 * @param uid       用户id
	 * @param noteData  笔记数据
	 * @param request
	 * @return          ModelAndView
	*/
	@RequestMapping(value = "/addNote")
	public ModelAndView addNote(@RequestParam(value = "noteFile", required = false) MultipartFile noteFile,
			@RequestParam("uid") int uid, @RequestParam("noteData") String noteData, HttpServletRequest request) {
		return noteService.addNoteToServer(noteFile, uid, noteData, request);
	}

	/**
	 * 删除一条或多条笔记功能
	 * @param noteIdsJson 笔记ids
	 * @param uid         用户id
	 * @param request
	 * @return            ModelAndView
	 */
	@RequestMapping(value = "/delNote",method = RequestMethod.POST)
	public ModelAndView deleteNote(@RequestParam("noteIdsJson") String noteIdsJson, @RequestParam("uid") int uid,@RequestParam("bookid") int bookid,
			HttpServletRequest request) {
		return noteService.delNoteAndZip(noteIdsJson, uid,bookid, request);
	}

	/**
	 * 获取笔记功能
	 * @param uid        用户id
	 * @param bookid     图书id
	 * @param noteJson   存的是本地有 的noteid和update_time
	 * @param request
	 * @return 数据信息
	*/
	@RequestMapping(value = "/getNoteData",method = RequestMethod.POST)
	public ModelAndView getNoteData(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("noteJson") String noteJson, HttpServletRequest request) {
		return noteService.getNoteData(uid, bookid, noteJson, request);

	}

	/**
	 * 获取笔记zip包功能
	 * @param uid       用户id
	 * @param bookid    图书id
	 * @param noteid    笔记id
	 * @param request   
	 * @return 成功、失败信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/getNoteZip",method = RequestMethod.POST)
	public ModelAndView getNoteZip(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("noteid") String noteid, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return noteService.getNoteZipFromServer(uid, bookid, noteid, request, response);
	}

	/**
	 * 将图书封面图片存到数据库中
	 * @param picFile 图片文件
	 * @param bookId  图书id
	 * @param request
	 * @return 成功失败信息
	*/
	@RequestMapping(value = "/addCoverPic", method = RequestMethod.POST)
	public ModelAndView updateCoverPic(@RequestParam("picFile") MultipartFile picFile,
			@RequestParam("bookid") int bookId, HttpServletRequest request) {
		return bookService.addCoverPic(picFile, bookId);
	}

	/**
	 * 书签同步功能
	 * @param uid
	 * @param json
	 * @param request
	 * @return 成功、失败信息（首选jsp试图，如果找不到找json试图）
	 */
	@RequestMapping(value = "/bookmarkSync",method = RequestMethod.POST)
	public ModelAndView bookmarkSync(@RequestParam("uid") int uid, @RequestParam("json") String json,
			HttpServletRequest request) {
		return bookmarkService.bookmarkSync(uid, json, request);
	}

	/**
	 * 阅读位置同步
	 * http://localhost:8080/ebook-server/book/readPositionSync?uid=xx&json=xx
	 * http://localhost:8080/ebook-server/book/readPositionSync.json
	 * post数据：uid=xx；json=xx
	 * @param uid
	 * @param json
	 * @param model
	 * @param request
	 * @return 首选jsp试图，如果找不到找json试图
	 */
	@RequestMapping(value = "/readPositionSync",method = RequestMethod.POST)
	public ModelAndView readPositionSync(@RequestParam("uid") int uid, @RequestParam("json") String json, Model model,
			HttpServletRequest request) {
		return bookmarkService.readPositionSync(uid, json, request);
	}

	/**
	 * 获取所有书签信息
	 * @param uid     用户名Id
	 * @param model
	 * @param request
	 * @return 视图
	*/
	@RequestMapping(value = "/getBookmark",method = RequestMethod.POST)
	public ModelAndView getBookmark(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			@RequestParam("markeridsJson") String markeridsJson, Model model, HttpServletRequest request) {
		return bookmarkService.getBookmark(uid, bookid, markeridsJson, request);
	}

	/**
	 * 根据书签
	 * @param uid
	 * @param markerid
	 * @param model
	 * @param request
	 * @return 成功失败信息
	*/
	@RequestMapping(value = "/deleteBookmark",method = RequestMethod.POST)
	public ModelAndView deleteBookmark(@RequestParam("uid") int uid, @RequestParam("markeridsJson") String markeridsJson,
			Model model, HttpServletRequest request) {
		return bookmarkService.deleteBookmark(uid, markeridsJson, request);
	}

	/**
	 * 获取一本书的阅读位置信息
	 * @param uid
	 * @param bookid
	 * @param model
	 * @param request
	 * @return 获取的信息
	*/
	@RequestMapping(value = "/getReadPosition",method = RequestMethod.POST)
	public ModelAndView getReadPosition(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid, Model model,
			HttpServletRequest request) {
		return bookmarkService.getReadPosition(uid, bookid, request);
	}

}
